package com.ant.androidBuddy;

import android.animation.Animator;
import android.content.Context;
import android.widget.FrameLayout.LayoutParams;
import android.view.Gravity;
import android.view.animation.*;
import android.animation.Animator.AnimatorListener;
import android.widget.*;


//is an extension of an ImageView
public class AndroidCharacter extends ImageView implements AnimatorListener{

	//list of all the coloured android drawables
	private static int[] colourList=null;

	//start at the first one
	public static int colourListIndex = 0;

	//the context is added by the animation activity
	public static Context context;

	//the layout to contain the android character is added by the animation activity
	public static FrameLayout layout;


	//constructor
	public AndroidCharacter(){
		super(context);
		//if the colour list is empty
		if(colourList==null){
			//populate it
			colourList=new int[5];
			colourList[0]=R.drawable.androidgreen;
			colourList[1]=R.drawable.androidblue;
			colourList[2]=R.drawable.androidpurple;
			colourList[3]=R.drawable.androidred;
			colourList[4]=R.drawable.androidorange;
		}
		//set the image for the right colour
		super.setImageResource(colourList[colourListIndex++]);
		//set the size of the android
		super.setLayoutParams(new LayoutParams(100, 110,Gravity.CENTER));
		//if at the end of the list of colours
		if(colourListIndex==colourList.length){
			//move back to the start
			colourListIndex=0;
		}
		//add this to the layout
		layout.addView(this);
		//add the animation listener
		super.animate().setListener(this);
	}


	//shows the android
	public void show(){
		super.setVisibility(VISIBLE);
		//interpret the next line
		Interpreter.readLine();
	}

	//hides the android
	public void hide(){
		super.setVisibility(INVISIBLE);
		//interpret the next line
		Interpreter.readLine();
	}

	// Spins Android 360 degrees
	public void spin() {
		super.animate().rotationYBy(360).setDuration(500).setInterpolator(new AccelerateDecelerateInterpolator());
	}

	// Moves Android left or right
	public void move(String direction, int distance) {
		//convert to pixels
		distance=(distance*layout.getWidth())/100;
		//if going left
		if (direction.contentEquals("l")) super.animate().setDuration(500).translationXBy(distance * -1);
		//if going right
		else if (direction.contentEquals("r")) super.animate().setDuration(500).translationXBy(distance);
	}

	
	// makes the android jump
	public void jump(String direction, int distance) {
		//convert to pixels
		distance=(distance*100)/100;
		//if going left
		if (direction.contentEquals("up")) super.animate().setDuration(500).translationYBy(distance * -1);
		//if going right
		else if (direction.contentEquals("down")) super.animate().setDuration(500).translationYBy(distance);
	}
	
	
	/*// Makes Android jump up
	public void jumpUp() {
		int distance=100;
		super.animate().translationXBy(distance*-1);//.setDuration(250).setInterpolator(new AccelerateDecelerateInterpolator());
	}

	// Makes Android jump back down
	public void jumpDown() {
		int distance=100;
		super.animate().translationXBy(distance);//.setDuration(250).setInterpolator(new AccelerateDecelerateInterpolator());
	}*/

	//set the colour of the android
	public void setColour(String colour) {
		if(colour.contentEquals("green")){
			super.setImageResource(R.drawable.androidgreen);
		}
		else if(colour.contentEquals("blue")){
			super.setImageResource(R.drawable.androidblue);
		}
		else if(colour.contentEquals("purple")){
			super.setImageResource(R.drawable.androidpurple);
		}
		else if(colour.contentEquals("red")){
			super.setImageResource(R.drawable.androidred);
		}
		else if(colour.contentEquals("orange")){
			super.setImageResource(R.drawable.androidorange);
		}
		//interpret the next line
		Interpreter.readLine();
	}
	
	//when an animation is finished
	public void onAnimationEnd(Animator animation) {
		//interpret the next line
		Interpreter.readLine();
	}
	
	//unused AnimatorListener methods
	public void onAnimationCancel(Animator animation) {}
	public void onAnimationRepeat(Animator animation) {}
	public void onAnimationStart(Animator animation) {}
}